#
# MakeFigures.R
#
# Replication file for
# Hill, Seth J. and Gregory A. Huber. 2018. ``On The Meaning of Survey Reports of Roll Call `Votes','' American Journal of Political Science.
#

# Editted for SI-C test in Race, Responsiveness and Representation by G. Agustin Markarian

rm(list=ls())
if (.Platform$OS == "windows") {
  # Set working directory in location of script.
  .doit <- function() { # only works with R.exe; trap errors if using Rscript.exe
  frame_files <- lapply(sys.frames(), function(x) x$ofile);frame_files <- Filter(Negate(is.null), frame_files)
  PATH <- dirname(frame_files[[length(frame_files)]]); setwd(PATH) ; rm(PATH,frame_files)
  }
  try(.doit(),silent=T)
}
library(bit64)
library(data.table)
options(stringsAsFactors=F)
library(RColorBrewer)
palette(brewer.pal(n=6,name="PiYG"))
par(cex.axis=1.2,cex.lab=1.2)

#
# %%%%%%%%%%%%%%%%%%%%%%%%
# Data outsheeted from 02_02_CCES2014_PerformCoreAnalysis.do
# %%%%%%%%%%%%%%%%%%%%%%%%
#
DT <- fread("Intermediate Data/HillHuberSurveyMeasure_ReplicationArchive/SupportByBillTreatPid.csv")
DT <- DT[bill != "",]

#
# %%%%%%%%%%%%%%%%%%%%%%%%
# Comparison of information treatment to standard
# support and congressional vote by bill and party.
# %%%%%%%%%%%%%%%%%%%%%%%%
#

.slope <- function(x,y1,y2,pch=19,lab="D",col=2) {
  # Create a sloping line connecting two points.
  y1 <- unlist(y1)
  y2 <- unlist(y2)
  x0 <- x - 0.5
  x1 <- x + 0.5
  segments(x0=x0,x1=x1,y0=y1,y1=y2,lwd=2)
  points(x=c(x0,x1),y=c(y1,y2),pch=pch,col=col,cex=1.5)
  text(x1,y2,pos=4,labels=lab)
}

.slopeSupportPlot <- function(DT, min.count=31, lvar="No=0", rvar="Yes=1", tvar=NULL,
                              labs=c("Black", "White", "Hispanic"), member.votes=TRUE) {
  # Make slope plot connecting support in two conditions.
  par.old <- par(mar=c(4.1,3.1,3.6,1.1))
  palette(brewer.pal(n=6, name="Set1"))
  
  # Open plot
  plot(x=c(1, DT[, length(unique(bill))]*3), y=c(0,1), type='n', ann=FALSE, axes=FALSE)
  axis(2, las=2)
  title(ylab="Proportion supporting bill", line=2.1)
  abline(h=0.5, col='gray')
  
  x <- 1
  for (bl in DT[, sort(unique(bill))]) {
    
    # Define race categories and aesthetics (excluding "Other")
    race_info <- list(
      Black    = list(col=2, pch=19, lab=labs[1]),
      White    = list(col=1, pch=15, lab=labs[2]),
      Hispanic = list(col=3, pch=17, lab=labs[3])
    )
    
    for (race in names(race_info)) {
      counts <- c(
        DT[bill == bl & treatment == lvar & race_cat == race, n],
        DT[bill == bl & treatment == rvar & race_cat == race, n]
      )
      if (all(counts >= min.count)) {
        .slope(
          x = x,
          y1 = DT[bill == bl & treatment == lvar & race_cat == race, vote_yea],
          y2 = DT[bill == bl & treatment == rvar & race_cat == race, vote_yea],
          lab = race_info[[race]]$lab,
          col = race_info[[race]]$col,
          pch = race_info[[race]]$pch
        )
      }
    }
    
    if (member.votes & rvar != "votec_split") {
      points(x=x+1.5, y=DT[bill == bl, dvotesfor][1]/100, col=2, pch=1, cex=1.5)
      points(x=x+1.5, y=DT[bill == bl, rvotesfor][1]/100, col=1, pch=0, cex=1.5)
    }
    
    if (!is.null(tvar)) {
      text(x=x+.5, y=0.05, labels=sprintf("t=%1.1f", DT[bill == bl & treatment == rvar, tvar, with=FALSE][1,]), pos=1)
    }
    
    axis(ifelse(x%%2==0, 1, 3), at=x+.5, labels=paste(strwrap(bl, 15), collapse="\n"),
         tick=FALSE, cex.axis=0.8, line=ifelse(x%%2==0, 2, -1))
    abline(v=x+2, lty=2)
    x <- x+3
  }
  
  par(par.old)
}

# All info.
DT <- fread("Intermediate Data/HillHuberSurveyMeasure_ReplicationArchive/SupportByBillTreatPid.csv")
DT <- DT[bill != "",]
pdf("Figure01InfoVsStndSupport.pdf",width=10,height=6)
.slopeSupportPlot(DT,tvar="did_t")
dev.off()